//
//  TVTopicTableViewCell.m
//  iPhoneXMPP
//
//  Created by MY053 on 14-12-9.
//  Copyright (c) 2014年 XMPPFramework. All rights reserved.
//
#define ScreenWidth [UIScreen mainScreen].bounds.size.width


#import "TVTopicTableViewCell.h"
#import "UIImageView+AFNetworking.h"

@implementation TVTopicTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)buildOtherCell:(StationMessageObject *)message
{
    //文本
    [self buildOtherText:message];
}

- (void)buildMyCell:(StationMessageObject *)message
{
     [self buildMyText:message];
}

- (void)buildOtherText:(StationMessageObject *)message
{
    //其他人
    CGRect rect = [self drawRectWithString:message.detail];
    //头像
    self.userIcon = [[UIImageView alloc] init];
  
    self.userIcon.layer.masksToBounds = YES;
    self.userIcon.layer.cornerRadius = 20;
    [self.userIcon setImageWithURL:[NSURL URLWithString:message.senderImgUrl]];
    self.userIcon.userInteractionEnabled=YES;
    
    [self addSubview:self.userIcon];
    [self.userIcon setFrame:CGRectMake(10, 10, 40, 40)];
    
    //名字
    self.userName = [[UILabel alloc] init];
    [self.userName setFrame:CGRectMake(60, 5, 200, 20)];
    [self.userName setText:message.senderName];
    [self.userName setFont:[UIFont systemFontOfSize:13]];
    [self.userName setTextColor:[UIColor lightGrayColor]];
    [self.userName setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:self.userName];
    UIImage *img = [[UIImage imageNamed:@"bubble_manage"] stretchableImageWithLeftCapWidth:20 topCapHeight:25];
    self.bgView = [[UIImageView alloc] init];
    [self.bgView setFrame:CGRectMake(60 , 30, rect.size.width + 20, rect.size.height + 16)];
    [self.bgView setImage:img];
    [self addSubview:self.bgView];
    
    //内容
    self.contentLabel = [[UILabel alloc] init];
    [self.contentLabel setFont:[UIFont systemFontOfSize:15]];
    [self.contentLabel setFrame:CGRectMake(13, 8, rect.size.width, rect.size.height)];
    [self.contentLabel setNumberOfLines:0];
    self.contentLabel.textAlignment =NSTextAlignmentCenter;
    [self.contentLabel setText:message.detail];
    [self.bgView addSubview:self.contentLabel];
}


- (void)buildMyText:(StationMessageObject *)message
{
    //其他人
    CGRect rect = [self drawRectWithString:message.detail];
    //头像
    self.userIcon = [[UIImageView alloc] init];
   
    self.userIcon.layer.cornerRadius = 20;
    [self addSubview:self.userIcon];
    [self.userIcon setFrame:CGRectMake(ScreenWidth - 50, 10, 40, 40)];
    [self.userIcon setImageWithURL:[NSURL URLWithString:message.senderImgUrl]];
     self.userIcon.layer.masksToBounds = YES;
    //名字
    self.userName = [[UILabel alloc] init];
    [self.userName setFrame:CGRectMake(ScreenWidth - 60 - 200, 5, 200, 20)];
    [self.userName setText:message.senderName];
    [self.userName setFont:[UIFont systemFontOfSize:13]];
    [self.userName setTextColor:[UIColor lightGrayColor]];
    [self.userName setTextAlignment:NSTextAlignmentRight];
    [self addSubview:self.userName];
    
    //背景图
    UIImage *img = [[UIImage imageNamed:@"bubble_self"] stretchableImageWithLeftCapWidth:20 topCapHeight:25];
    self.bgView = [[UIImageView alloc] init];
    [self.bgView setFrame:CGRectMake(ScreenWidth - 60 - rect.size.width - 20, 30, rect.size.width + 20, rect.size.height + 16)];
    [self.bgView setImage:img];
    [self addSubview:self.bgView];
    
    //内容
    self.contentLabel = [[UILabel alloc] init];
    [self.contentLabel setFont:[UIFont systemFontOfSize:15]];
    [self.contentLabel setFrame:CGRectMake(8, 8, rect.size.width, rect.size.height)];
    [self.contentLabel setNumberOfLines:0];
    [self.contentLabel setText:message.detail];
    [self.bgView addSubview:self.contentLabel];
    
    self.indicatorView = [[UIActivityIndicatorView alloc] init];
    [self.indicatorView setFrame:CGRectMake(-25, self.bgView.frame.size.height / 2 - 10, 20, 20)];
    [self.indicatorView setColor:[UIColor blueColor]];
    [self.bgView addSubview:self.indicatorView];
}

//获得行高
- (CGRect )drawRectWithString:(NSString *)text
{
    //宽度
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange allRange = [text rangeOfString:text];
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:15]
                    range:allRange];
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor blackColor]
                    range:allRange];
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(self.frame.size.width - 140, CGFLOAT_MAX)
                                        options:options
                                        context:nil];
    return rect;
}

@end
